package com.meilele.controller.czh;

import com.meilele.biz.czh.FiveCommentariesBiz;
import com.meilele.biz.czh.FivePostBiz;
import com.meilele.entity.czh.FiveCommentaries;
import com.meilele.entity.czh.FivePost;
import com.meilele.entity.czh.vo.FiveRcpVo;
import com.meilele.entity.czh.vo.FiveZhuang;
import com.meilele.entity.shiro.User;
import com.meilele.util.czh.Constants;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/fivepost")
public class FivePostController {
    @Resource
    private FivePostBiz fivePostBiz;
    @Resource
    private FiveCommentariesBiz fiveCommentariesBiz;
    @RequestMapping("/lista/{pageNum}")
    public String listsheji(@PathVariable("pageNum")int pageNum, Model model){
        List<FiveZhuang> list=fivePostBiz.findPosta(pageNum);
       model.addAttribute("list",list);
       int totalCount=fivePostBiz.findCounta();
       double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
       int total=(int) Math.ceil(totalPage);
       model.addAttribute("total",total);
       model.addAttribute("pageNum",pageNum);
        return "/fivepost/lista";
    }
    @RequestMapping("/listb/{pageNum}")
    public String listb(@PathVariable("pageNum")int pageNum, Model model){
        List<FiveZhuang> list=fivePostBiz.findPostb(pageNum);
        model.addAttribute("list",list);
        int totalCount=fivePostBiz.findCountb();
        double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
        int total=(int) Math.ceil(totalPage);
        model.addAttribute("total",total);
        model.addAttribute("pageNum",pageNum);
        return "/fivepost/listb";
    }
    @RequestMapping("/listc/{pageNum}")
    public String listc(@PathVariable("pageNum")int pageNum, Model model){
        List<FiveZhuang> list=fivePostBiz.findPostc(pageNum);
        model.addAttribute("list",list);
        int totalCount=fivePostBiz.findCountc();
        double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
        int total=(int) Math.ceil(totalPage);
        model.addAttribute("total",total);
        model.addAttribute("pageNum",pageNum);
        return "/fivepost/listc";
    }
    @RequestMapping("/listd/{pageNum}")
    public String listd(@PathVariable("pageNum")int pageNum, Model model){
        List<FiveZhuang> list=fivePostBiz.findPostd(pageNum);
        model.addAttribute("list",list);
        int totalCount=fivePostBiz.findCountd();
        double totalPage=totalCount*1.0/ Constants.PAGE_SIZE;
        int total=(int) Math.ceil(totalPage);
        model.addAttribute("total",total);
        model.addAttribute("pageNum",pageNum);
        return "/fivepost/listd";
    }
    @RequestMapping("/preadd")
    public String preadd(){
        return "/fivepost/add";
    }
    @RequestMapping("/add")
    public String add(FivePost fivePost, HttpSession session){
        User user=(User) session.getAttribute("user");
        String username=user.getUsername();
        fivePost.setPostName(username);
        Date date=new Date();
        fivePost.setCreateTime(date);
        fivePostBiz.save(fivePost);
        return "/fivepost/lasthead";
    }
    @RequestMapping("/lasthead")
    public String lasthead(){
        return "/fivepost/lasthesd";
    }
    @RequestMapping("/deatils/{postId}")
    public String details(@PathVariable("postId")int postId,Model model){
       FiveRcpVo fiveRcpVo=fivePostBiz.findRcp(postId);
        model.addAttribute("fiveRcpVo",fiveRcpVo);
        return "/fivepost/details";
    }
    @RequestMapping("/commadd")
    public String commadd(FiveCommentaries fiveCommentaries,HttpSession session){
        Date date=new Date();
        fiveCommentaries.setCommentariesTime(date);
        User user=(User) session.getAttribute("user");
        String username=user.getUsername();
        fiveCommentaries.setCommentariesName(username);
       fiveCommentariesBiz.save(fiveCommentaries);
       String aa="redirect:/fivepost/deatils/"+fiveCommentaries.getPostId();
       return aa;
    }
}
